#!/usr/bin/python
from __future__ import with_statement
import os, sys

PATH='/opt/9263/toolchain/usr/bin/'

if not os.path.exists(PATH):
	raise Exception(PATH+' not exists.')

if os.getuid()!=0:
	raise Exception('Run me by `sudo`.')

with open("/etc/environment","r+") as f:
	contents=f.readlines()
	indices = xrange(sys.maxint)
	found=False
	for c,index in zip(contents,indices):
		if c.find('PATH=')==0:
			found=True
			if not PATH in c:
				contents[0]=contents[0].rstrip()[:-1]+(":%s\"\n"%PATH)
				f.seek(0,0)
				f.writelines(contents)

	if not found: raise Exception('No `PATH=` found.')

print 'Update /etc/environment successfully. Require rebooting.'
